import path from "path"
import json from "@rollup/plugin-json"
import resolvePlugin from "@rollup/plugin-node-resolve"
import {fileURLToPath} from "url";
import fs from "fs"
import typescript from "@rollup/plugin-typescript";

// c:\Users\18103\Desktop\新建文件夹\vue-yuanma\packages
let p = fileURLToPath(import.meta.url);
let packagesDir = path.resolve(path.dirname(p), "packages")
// @ts-ignore
let packageDir = path.resolve(packagesDir, process.env.TARGET)

const name = path.basename(packageDir)
const resolve = p => path.resolve(packageDir, p)

const packageContent = fs.readFileSync(resolve(`package.json`), 'utf8');

const outputConfigs = {
    'esm-bundler': {
        file: resolve(`dist/${name}.esm-bundler.js`),
        format: `es`
    },
    cjs: {
        file: resolve(`dist/${name}.cjs.js`),
        format: `cjs`
    },
    global: {
        file: resolve(`dist/${name}.global.js`),
        format: `iife`
    }
}
// @ts-ignore
const packageOptions = JSON.parse(packageContent).buildOptions

function createConfig(format, output) {
    output.name = packageOptions.name
    output.sourcemap = true
    return {
        input: resolve("src/index.ts"),
        output,
        plugins: [
            json(),
            typescript({
                tsconfig: path.resolve(path.dirname(p), "tsconfig.json")
            }),
            resolvePlugin()
        ]
    }
}

// @ts-ignore
export default packageOptions.formats.map(format => createConfig(format, outputConfigs[format]))

